//
//  InvoiceViewController.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/26.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
protocol InvoiceViewControllerDelegate: NSObjectProtocol {
    func invoiceSave(invoiceTitle: String, taxpayerIdNo :String, unitName: String, invoiceContent: String)
}
class InvoiceViewController: BaseViewController, BottomShadow, AddCornerUIRectCorner,UITextFieldDelegate {
    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var topView: UIView!
    @IBOutlet weak var bottomView: UIView!
    @IBOutlet weak var invoiceTypeButton: UIButton!
    @IBOutlet weak var personalButton: UIButton!
    @IBOutlet weak var companyButton: UIButton!
    @IBOutlet weak var firstTextField: UITextField!
    @IBOutlet weak var secondView: UIView!
    @IBOutlet weak var secondTextField: UITextField!
    @IBOutlet weak var invoiceContentButton: UIButton!
    @IBOutlet weak var invoiceTitleWithPersonal: NSLayoutConstraint!
    @IBOutlet weak var invoiceTitleWithNumber: NSLayoutConstraint!
    @IBOutlet weak var totalPriceLabel: UILabel!
    
    @IBOutlet weak var serviceInfoLabel: UILabel!
    weak var invoiceDelegate:InvoiceViewControllerDelegate?
    
    @IBOutlet weak var mingxiBtn: UIButton!
    
    @IBOutlet weak var categoryBtn: UIButton!
    
    var price :String?
    var wx :String?
    var tel :String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        // Do any additional setup after loading the view.
    }
    
    
    
    func setUI() {
        secondView.isHidden = true
        self.titleLabel(text: "发票信息", "Invoice_icon")
        firstTextField.attributedPlaceholder = NSAttributedString(string: "个人", attributes: [NSAttributedStringKey.foregroundColor : UIColor.lightGray, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14)])
        secondTextField.attributedPlaceholder = NSAttributedString(string: "请输入纳税人识别号", attributes: [NSAttributedStringKey.foregroundColor : UIColor.lightGray, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14)])
        setShadow(view: mainView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        topView.layer.zPosition = 1.0
        setShadow(view: topView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        addCornerWithView(aView: bottomView, aCorners: [.bottomLeft, .bottomRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        bottomView.layer.zPosition = 0.9
        
        let buttons = [personalButton, companyButton, invoiceContentButton]
        let _ = buttons.map({
            let color = $0?.currentTitle == "个人" ? ONColor.currencyRedColor : ONColor.unSelectColor
            $0?.layer.cornerRadius = invoiceTypeButton.height / 2
            $0?.layer.borderColor = color.cgColor
            $0?.layer.borderWidth = 1
        })
        
        let btns = [mingxiBtn, categoryBtn]
        let _ = btns.map({
            let color = $0?.currentTitle == "商品明细" ? ONColor.currencyRedColor : ONColor.unSelectColor
            $0?.layer.cornerRadius = mingxiBtn.height / 2
            $0?.layer.borderColor = color.cgColor
            $0?.layer.borderWidth = 1
        })
        
        totalPriceLabel.attributedText = NSMutableAttributedString.differentColorAndSizeString(first: (firstString: "￥", firstColor: UIColor.black, firstSize: 13), second: (secondString: price as! NSString, secondColor: ONColor.currencyRedColor, secondSize: 15))
        if !(wx?.isEmpty)! && !(tel?.isEmpty)! {
            serviceInfoLabel.text = "客服微信号：\(wx!)\n客服联系电话：\(tel!)"
        }
    
    }
    
    override func setNavigation() {
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "btn_save"), style: .done, target: self, action: #selector(saveTap))
    }
    
    // MARK: - 保存
    @objc func saveTap() {
        var content:String = "商品明细"
        if mingxiBtn.isSelected {
            content = (mingxiBtn.titleLabel?.text)!
        }else if categoryBtn.isSelected {
            content = (categoryBtn.titleLabel?.text)!
        }
        
        var taitou:String = "个人"
        if personalButton.isSelected {
            taitou = (personalButton.titleLabel?.text)!
        }else if companyButton.isSelected {
            taitou = (companyButton.titleLabel?.text)!
        }
        self.invoiceDelegate?.invoiceSave(invoiceTitle: taitou, taxpayerIdNo: firstTextField.text!, unitName: secondTextField.text!, invoiceContent: content)
        self.navigationController?.popViewController(animated: true)
    }
    
    // MARK: - 发票类型
    @IBAction func invoiceTypeTap(_ sender: UIButton) {
    }
    
    // MARK: - 发票抬头
    @IBAction func invoiceTitleButtonTap(_ sender: UIButton) {
        let buttons = [companyButton, personalButton]
        for button in buttons {
            buttonColor(btn: button!, isSelect: sender == button)
        }
        
        let personalpriority: UILayoutPriority = sender.tag == 0 ? .defaultHigh : .defaultLow
        let numberpriority: UILayoutPriority = sender.tag == 0 ? .defaultLow : .defaultHigh
        secondView.isHidden = sender.tag == 0
        invoiceTitleWithPersonal.priority = .init(personalpriority.rawValue)
        invoiceTitleWithNumber.priority = .init(numberpriority.rawValue)
        let text = sender.tag == 0 ? "个人" : "请输入单位名称"
        firstTextField.attributedPlaceholder = NSAttributedString(string: text, attributes: [NSAttributedStringKey.foregroundColor : UIColor.lightGray, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14)])
    }
    
    @IBAction func catergoryAct(_ sender: UIButton) {
        let buttons = [mingxiBtn, categoryBtn]
        for button in buttons {
            buttonColor(btn: button!, isSelect: sender == button)
        }
        
    }
    private func buttonColor(btn: UIButton, isSelect: Bool) {
        let color = isSelect ? ONColor.currencyRedColor : ONColor.unSelectColor
        btn.layer.borderWidth = 1
        btn.layer.borderColor = color.cgColor
        btn.setTitleColor(color, for: .normal)
        btn.isSelected = isSelect
    }
    
    // MARK: - 发票内容
    @IBAction func invoiceContentTap(_ sender: UIButton) {
        
    }
    
    public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let length = string.lengthOfBytes(using: String.Encoding.utf8)
        if length == 0 {
            return true
        }
        for loopIndex in 0..<length {
            
            let char = (string as NSString).character(at: loopIndex)
            
            if char >= 48 && char <= 57 {
                return true
            }
            if char >= 65 && char <= 90 {
                return true
            }
            if char >= 97 && char <= 122 {
                return true
            }
            
        }
        return false
    }
    
}
